/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.toools.gwt.wmc.server.waste;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import java.util.Collection;
import org.toools.gwt.wmc.client.waste.WasteService;
import org.toools.gwt.wmc.server.dao.IWasteDAO;
import org.toools.gwt.wmc.jpa.Waste;
import org.toools.gwt.wmc.server.WMCSession;

/**
 *
 * @author Manuel
 */
public class WasteServiceImpl extends RemoteServiceServlet implements WasteService {

    private IWasteDAO wastedao;

    /** Account DAO **/
    public void setWastedao( IWasteDAO dao ) {
        this.wastedao = dao;
    }
    public IWasteDAO getWastedao() {
		return wastedao;
	}


    public Waste getWasteByID(Integer ID) {
        if( WMCSession.isAdmin( getThreadLocalRequest() ) || wastedao.belongsTo( ID, WMCSession.getAccountID( getThreadLocalRequest() ) ) )
            return wastedao.getWasteByID( ID );

        return null;
    }

    public Collection<Waste> getWastes() {
        if( WMCSession.isAdmin( getThreadLocalRequest() ) )
                return wastedao.getWastes();

        return wastedao.getWastes( WMCSession.getAccountID( getThreadLocalRequest() ) );
    }

    public Waste create(Waste d) {
        if( WMCSession.isAdmin( getThreadLocalRequest() ) )
            return wastedao.create( d );
        //check if belongs to account
        else
            return wastedao.create( d );
    }

    public Boolean update(Waste d) {
        if( WMCSession.isAdmin( getThreadLocalRequest() ) || wastedao.belongsTo( d.getGtin(), WMCSession.getAccountID( getThreadLocalRequest() ) ) )
            return wastedao.update( d );
        return false;
    }

    public Boolean delete(Waste d) {
        if( WMCSession.isAdmin( getThreadLocalRequest() ) || wastedao.belongsTo( d.getGtin(), WMCSession.getAccountID( getThreadLocalRequest() ) ) )
            return wastedao.delete( d );
        return false;
    }
}
